package com.lucian.ge.hatchdo.www.controller;

import io.swagger.annotations.*;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.lucian.ge.comman.model.ResultMap;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;

/**
 * Created by Lucicol on 2019/6/12.
 */
@Api(value = "MailController", description = "官网邮件服务")
@RestController
@RequestMapping("/mail")
@Validated
public class MailController {
    private final ResultMap resultMap;

    @Autowired
    private JavaMailSender mailSender;
    public MailController(ResultMap resultMap) {
        this.resultMap = resultMap;
    }

    /**
     * 官网joinUs邮箱
     */

    @ApiOperation(value="官网joinUs邮箱", notes="")
//    @CrossOrigin(origins = {"http://localhost:8080", "null"})
    @PostMapping("/joinus")
//    @ResponseBody
//    public ResultMap joinUs(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) @Valid MailInfo mailInfo, BindingResult result) {
    public ResultMap joinUs(@Valid
                                @RequestParam(value = "phonenub")
                                @Pattern(regexp = "1[3|4|5|7|8][0-9]\\d{8}",message="手机号格式错误")
                                @NotEmpty(message="手机号不能为空")
                                        String phoneNub,
                            @RequestParam(value = "name", required = false)
//                                @NotEmpty(message="姓名不能为空")
//                                @Length(max = 20, message = "姓名长度限制最多20字符")
                                    String name,
                            @RequestParam(value = "wx", required = false)
                                        String wx,
                            @RequestParam(value = "mail", required = false)
                                        String mail,
                            @RequestParam(value = "age", required = false)
                                        String age,
                            @RequestParam(value = "dir", required = false)
                                        String dir
                            ) {
        //      参数校验
//        if (result.hasErrors()) {
//            return resultMap.fail().code(401).message(result.getFieldError().getDefaultMessage());
//        }
//        System.out.println(mailInfo.getName());
//        System.out.println(mailInfo.getPhoneNub());

        System.out.println(phoneNub);
        try {
            final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setFrom("3486477503@qq.com");
//            message.setTo("td@hatchdo.com");
            message.setTo("3486477503@qq.com");
            message.setSubject("官网联系我们join us,姓名,电话");
            message.setText("姓名:"+ name+ "," + "电话:" + phoneNub+","+"微信:"+wx+"邮箱:"+mail+"年龄:"+age+"地址:"+dir);
//            message.setText("姓名:"+ mailInfo.getName()+ "," + "电话:" + mailInfo.getPhoneNub());
            this.mailSender.send(mimeMessage);
            return resultMap.success().code(200).message("成功发送邮件").token("").res("").data(null);
        } catch (Exception ex) {
            ex.printStackTrace();
//            return "error";
            return resultMap.fail().code(401).message("发送邮件失败").token("").res("").data(null);
        }
    }

    /**
     * 官网joinUs邮箱
     */

    @ApiOperation(value="落地页一", notes="")
//    @CrossOrigin(origins = {"http://localhost:8080", "null"})
    @PostMapping("/adone")
//    @ResponseBody
//    public ResultMap joinUs(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) @Valid MailInfo mailInfo, BindingResult result) {
    public ResultMap adOne(@Valid
                            @RequestParam(value = "phonenub")
                            @Pattern(regexp = "1[3|4|5|7|8][0-9]\\d{8}",message="手机号格式错误")
                            @NotEmpty(message="手机号不能为空")
                                    String phoneNub,
                            @RequestParam(value = "name")
                                @NotEmpty(message="姓名不能为空")
                                @Length(max = 20, message = "姓名长度限制最多20字符")
                                    String name,
                            @RequestParam(value = "dir")
                               @NotEmpty(message="区域不能为空")
                               @Length(max = 50, message = "区域长度限制最多50字符")
                                    String dir
    ) {
        //      参数校验
//        if (result.hasErrors()) {
//            return resultMap.fail().code(401).message(result.getFieldError().getDefaultMessage());
//        }
//        System.out.println(mailInfo.getName());
//        System.out.println(mailInfo.getPhoneNub());

        System.out.println(phoneNub);
        try {
            final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setFrom("3486477503@qq.com");
//            message.setTo("td@hatchdo.com");
            message.setTo("3486477503@qq.com");
            message.setSubject("落地页一,姓名,电话");
            message.setText("姓名:"+ name+ "," + "电话:" + phoneNub+","+"区域:"+dir);
//            message.setText("姓名:"+ mailInfo.getName()+ "," + "电话:" + mailInfo.getPhoneNub());
            this.mailSender.send(mimeMessage);
            return resultMap.success().code(200).message("成功发送邮件").token("").res("").data(null);
        } catch (Exception ex) {
            ex.printStackTrace();
//            return "error";
            return resultMap.fail().code(401).message("发送邮件失败").token("").res("").data(null);
        }
    }
}

//@ApiModel
//class MailInfo {
//    @ApiModelProperty(value = "手机号")
//    @NotEmpty(message="手机号不能为空")
//    @Pattern(regexp = "1[3|4|5|7|8][0-9]\\d{8}",message="手机号格式错误")
//    private String phoneNub;
//
//    @ApiModelProperty(value = "还款对象")
//    @NotEmpty(message="姓名不能为空")
//    @Length(max = 20, message = "姓名长度限制最多20字符")
//    private String name;
//
//    public String getPhoneNub() {
//        return phoneNub;
//    }
//
//    public void setPhoneNub(String phoneNub) {
//        this.phoneNub = phoneNub;
//    }
//
//    public String getName() {
//        return name;
//    }
//
//    public void setName(String name) {
//        this.name = name;
//    }
//}
